//
//  BTLifeViewController.m
//  blanket
//
//  Created by Mac on 2018/11/1.
//  Copyright © 2018 stoneobs@icloud.com. All rights reserved.
//

#import "BTLifeViewController.h"
#import "BLLiveHeaderView.h"
#import "BLLifeCollectionViewCell.h"
#import "BLLifeCateViewController.h"
#import "BLShopModel.h"
#import "BLShopingDetailViewController.h"
@interface BTLifeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) BLLiveHeaderView                     *header;/**< <##> */
@property(nonatomic, strong) UICollectionView                     *collectionView;/**< <##> */
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**< <##> */
@end

@implementation BTLifeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    self.title = @"铺盖生活";
    [self configSubView];
    __weak typeof(self) weakSelf =  self;
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf sendListRequest];
    }];
    [self.collectionView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
        flow.minimumLineSpacing = 5;
        flow.minimumInteritemSpacing = 5;
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        flow.sectionInset = UIEdgeInsetsMake(self.header.height,12 ,0, 12);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, TMUtils.navigationBar.bottom, UIScreenWidth, TMUtils.tabbar.top + 49 - TMUtils.navigationBar.bottom) collectionViewLayout:flow];
        _collectionView.backgroundColor = UIColor.whiteColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        //注册
        [_collectionView registerClass:[BLLifeCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
        if (ios11) {
            if ([_collectionView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
                [_collectionView performSelector:@selector(setContentInsetAdjustmentBehavior:) withObject:@(2) afterDelay:0];
            }
        }
        
    }
    return _collectionView;
}
#pragma mark --subView
- (void)configSubView{
    BLLiveHeaderView * heder = [[BLLiveHeaderView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 30)];
//    heder.top = TMUtils.navigationBar.bottom;
    heder.bannerArray = self.bannerArray;
    heder.cidArray = self.cidArray;
    [heder resetSubView];
    __weak typeof(self) weakSelf =  self;
    self.header = heder;
    [_header setOnSlectedControl:^(NSString * _Nonnull cid, NSString * _Nonnull cidName) {
        BLLifeCateViewController * vc = BLLifeCateViewController.new;
        vc.cid = cid;
        vc.title = cidName;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    [self.collectionView addSubview:heder];
    [self.view addSubview:self.collectionView];
}
#pragma --mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSouce.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat witdh = (UIScreenWidth - 24 - 5)/2;
    if (indexPath.row == 0) {
      return  CGSizeMake(UIScreenWidth - 25, 130);
    }
    return CGSizeMake(witdh, witdh);
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BLLifeCollectionViewCell * item =[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];

    BLShopModel * model = self.dataSouce[indexPath.row];
    [item.bannerImageView sd_setImageWithURL:[NSURL URLWithString:model.img_url.firstObject]];
    item.titleLable.text = model.name;
    item.moenLable.text = [NSString stringWithFormat:@"%@元",model.price];
    item.pointLable.text = [NSString stringWithFormat:@"%ld元 + %@积分",
                            model.price.integerValue - model.score.integerValue,model.score];
    
    return item;
}
#pragma --mark UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BLShopModel * model = self.dataSouce[indexPath.row];
    BLShopingDetailViewController * vc = BLShopingDetailViewController.new;
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
//    [self.navigationController pushViewController:BLLifeCateViewController.new animated:YES];
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
- (void)sendListRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/Api/Basics/getGatherScoreList"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [dic setObject:@"1" forKey:@"is_show"];
     [dic setObject:@"1" forKey:@"page"];
     [dic setObject:@"1" forKey:@"type"];
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.collectionView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 self.dataSouce = NSMutableArray.new;
                                                NSArray * array = responseObject[@"data"];
                                                 for (NSDictionary * modelDic in array) {
                                                     BLShopModel * model = [BLShopModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.collectionView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.collectionView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
//                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
